//
//  LDUtils.swift
//  DingDongZP
//
//  Created by 三秒文化 on 2016/10/31.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit

@objc(LDUtils) class LDUtils: NSObject {

    
    enum ValidatedType {
        case Email
        case PhoneNumber
        case Url
    }
    
    
    //emial
    class func validateEmail(vStr: String) -> Bool {
        
        return validateText(validatedType: ValidatedType.Email, validateString: vStr)
    }
    
    //phone
    class func validatePhone(vStr: String) -> Bool {
        
        return validateText(validatedType: ValidatedType.PhoneNumber, validateString: vStr)
    }
    //url
    class func validateUrl(vStr: String) -> Bool {
        
        return validateText(validatedType: ValidatedType.Url, validateString: vStr)
    }
    
    class func validateText(validatedType type: ValidatedType, validateString: String) -> Bool {
        
        do {
            
            var pattern: String!
            var tempStr = validateString
            
            if type == ValidatedType.Email {
                
                pattern = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
            } else if type == ValidatedType.PhoneNumber {
                
                pattern = "^1[0-9]{10}$"
            } else if type == ValidatedType.Url {
                
                if validateString.hasPrefix("www") {
                    
                    tempStr = "http://" + validateString
                }
                
                pattern = "\\bhttps?://[a-zA-Z0-9\\-.]+(?::(\\d+))?(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?"
//                pattern =  String.init("%@",)//
            }
            
            let regex: NSRegularExpression = try NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive)
            let matches = regex.matchesInString(tempStr, options: NSMatchingOptions.ReportProgress, range: NSMakeRange(0, validateString.characters.count))
            return matches.count > 0
        }
        catch {
            
            return false
        }
    }
    
    //默认md5 + base64
    class func loginEncode(userName:NSString?,key:NSString?) ->NSString? {
        
        let nameStr = NSString(format: "egg%@", userName ?? "");
        let passStr = NSString(format: "%@", key ?? "");
        
        let md5Str:NSString = NSString.md5(nameStr as String)
        
        
        let charMd5 = md5Str.cStringUsingEncoding(NSUTF8StringEncoding)
        let md5length = strlen(charMd5)
        
        
        let charKey = passStr.cStringUsingEncoding(NSUTF8StringEncoding)
        let klength = strlen(charKey)
        
        let dst = UnsafeMutablePointer<Int8>(malloc(Int(md5length) + 1))
        
        debugPrint("klength---",klength)
        
        
        dst[Int(md5length)] = 0
        
        
        debugPrint("md5length",Int(md5length))
        
        
        for  i in 0..<Int(klength) {
            
            let md5Value = charMd5[i]
            let charValue = charKey[i]
            
            dst[i] = md5Value ^ charValue
            
            debugPrint(dst[i])
        }
        
        for i in Int(klength)..<Int(md5length) {
            
            dst[i] = charMd5[i]
        }
        
        
        let xorData = NSData.init(bytes: dst, length: Int(md5length))
        
        free(dst)
        
        let base64 = GTMBase64.stringByEncodingData(xorData)
    
        return base64
        
    }
}
